package com.yohann.boot.service.impl;

import com.yohann.boot.common.exception.GlobalException;
import com.yohann.boot.entity.TestElasticsearch;
import com.yohann.boot.mapper.TestElasticsearchRepository;
import com.yohann.boot.pojo.TestVO;
import com.yohann.boot.service.RabbitMqService;
import com.yohann.boot.service.TestService;
import com.yohann.boot.util.LogUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * 测试业务实现
 *
 * @author Yeung Wah
 * @since 2021/5/7
 */
@Service
public class TestServiceImpl implements TestService {
    @Resource(name = "threadPool")
    private ThreadPoolTaskExecutor executor;
    @Autowired
    private RabbitMqService rabbitMqService;
    @Autowired
    private TestElasticsearchRepository testElasticsearchRepository;

    @Value("${rabbit.mq.exchange}")
    private String exchangeName;
    @Value("${rabbit.mq.routing.key}")
    private String routingKeyName;

    @Override
    public Object test(TestVO vo) {
        try {
            GlobalException.throwOut("错误信息");
        } catch (Exception e) {
            LogUtils.error(e);
        }

        return Arrays.asList("a", "b", "c", "d");
    }

    @Override
    @SneakyThrows
    public void testThreadPoll() {
        Runnable runnable = () -> {
            System.out.println("runnable");
        };

        Runnable runnable2 = () -> System.out.println("runnable2");

        //异步任务
        for (int i = 0; i < 100; i++) {
            executor.execute(runnable);
        }

        //同步任务
        for (int i = 0; i < 100; i++) {
            executor.submit(runnable2);
        }

        //有返回值同步任务
        Callable<String> callable = () -> new Date().toString();
        for (int i = 0; i < 100; i++) {
            Future<String> future = executor.submit(callable);
            System.out.println(i + " " + "callable");
        }
    }

    @Override
    public void send(String message) {
        rabbitMqService.sendMessage(exchangeName, routingKeyName, message);
    }

    @Override
    public void send(String message, String ttl) {
        rabbitMqService.sendMessage(exchangeName, routingKeyName, message, ttl);
    }

    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String senderEmail;

    @Override
    public void sendEmail(String email) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(senderEmail);
            messageHelper.setTo(email);
            messageHelper.setSubject("Test");
            messageHelper.setText("测试", false);
            mailSender.send(message);
        } catch (Exception e) {
            GlobalException.throwOut("发送邮件失败:" + e.getMessage(), e);
        }
    }

    @Override
    public Iterable<TestElasticsearch> getEs(String content) {
        return testElasticsearchRepository.findAll();
    }

    @Override
    public void saveEs(TestElasticsearch elasticsearch) {
        testElasticsearchRepository.save(elasticsearch);
    }
}